<template>
    <!-- 详情页 -->
    <div class="zhu">
        <!-- 轮播图 -->
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
            <van-swipe-item v-for="item in gallery" :key="item.id">
                <img :src="item.img_url" alt="" />
            </van-swipe-item>
        </van-swipe>
        <!-- 返回上一页 -->
        <van-icon name="arrow-left" class="fanHui" @click="huiQu" />
        <!-- 分享插件 -->
        <div class="shareCss">
            <van-cell title="分享" @click="showShare = true" />
            <van-share-sheet v-model="showShare" title="立即分享给好友" :options="options" @select="onSelect" />
        </div>
        <p class="tuiHuo">
            <span>30天无忧退货</span>
            <span>30天无忧退货</span>
            <span>30天无忧退货</span>
        </p>
        <div class="introduce">
            {{ info }}
            <p class="p1">{{ goods_brief }}</p>
            <p class="p2" v-if="retail_price">{{ retail_price | formatPrice }}</p>
        </div>
        <!-- 商品参数 -->
        <div class="attribute">
            <h4>商品参数</h4>
            <ul>
                <li v-for="(item, index) in attribute" :key="index">
                    <span>{{ item.name }}</span>
                    <section class="van-ellipsis">{{ item.value }}</section>
                </li>
            </ul>
        </div>
        <!-- 产品详情 -->
        <div v-html="goods_desc" class="goods_desc"></div>
        <!-- 常见问题 -->
        <div class="title">
            <span>常见问题</span>
        </div>
        <ul class="issue">
            <li v-for="item in issue" :key="item.id">
                <h3>{{ item.question }}</h3>
                <p>{{ item.answer }}</p>
            </li>
        </ul>
        <div class="title">
            <span>相关产品</span>
        </div>
        <div class="newList">
            <ul>
                <li v-for="item in goodsList" :key="item.id">
                    <img v-lazy="item.list_pic_url" alt="" />
                    <span>{{ item.name }}</span>
                    <p>{{ item.retail_price | formatPrice }}</p>
                </li>
            </ul>
        </div>
        <!-- 底部加入购车 -->
        <van-goods-action>
            <van-goods-action-icon icon="chat-o" text="客服" @click="onClickButton" />
            <van-goods-action-icon icon="cart-o" text="购物车" @click="onClickIcon" />
            <van-goods-action-icon icon="shop-o" text="店铺" @click="onClickButton" />
            <van-goods-action-button type="danger" text="立即购买" @click="onClickButton" />
        </van-goods-action>
    </div>
</template>

<script>
import {
    getGoodDetail,
    getGoodRelated, //数据页列表
} from '@/utils/https';
export default {
    components: {},
    data() {
        return {
            showShare: false,
            options: [
                { name: '微信', icon: 'wechat' },
                { name: '微博', icon: 'weibo' },
                { name: '复制链接', icon: 'link' },
                { name: '分享海报', icon: 'poster' },
                { name: '二维码', icon: 'qrcode' },
            ],

            id: '',
            // 存照片的数组
            gallery: [],
            // 商品名字
            info: [],
            // 商品特性
            goods_brief: [],
            // 价格
            retail_price: '',
            // 图片
            goods_desc: [],
            // 商品参数
            attribute: [],
            // 常见问题
            issue: [],
            // 相关产品
            goodsList: '',
        };
    },
    created() {
        this.id = this.$route.query.id;
        getGoodDetail({ id: this.id }).then(res => {
            // console.log(res);
            this.gallery = res.data.data.gallery;
            this.info = res.data.data.info.name;
            this.goods_brief = res.data.data.info.goods_brief;
            this.retail_price = res.data.data.info.retail_price;
            this.goods_desc = res.data.data.info.goods_desc;
            this.attribute = res.data.data.attribute;
            this.issue = res.data.data.issue; //常见问题
        });
        getGoodRelated({ id: this.id }).then(res => {
            console.log(res);
            this.goodsList = res.data.data.goodsList;
            console.log(this.goodsList);
        });
    },
    methods: {
        // 返回上一页
        huiQu() {
            this.$router.go(-1);
        },
        // 分享插件
        onSelect(option) {
            Toast(option.name);
            this.showShare = false;
        },
        onClickIcon() {
            this.$router.push('/cart');
        },
        onClickButton() {},
    },
};
</script>

<style lang="less" scoped>
.zhu {
    padding-bottom: 100px;

    position: relative;

    .my-swipe {
        height: 300px;
        color: #fff;
        font-size: 20px;
        line-height: 150px;
        text-align: center;
        background-color: #39a9ed;
    }
    img {
        width: 100%;
    }
    .fanHui {
        position: absolute;
        top: 10px;
        font-size: 40px;
    }
    .van-cell {
        border: 1px solid black;
        // border-radius: 30%;
        width: 40px;
        position: absolute;
        right: 0;
        top: 0;
        opacity: 0.3;
    }
    .tuiHuo {
        font-size: 16px;
        margin-left: 5px;
        span {
            display: inline-block;
            margin: 0 5px;
        }
    }
    .introduce {
        text-align: center;
        font-size: 15px;
        margin-top: 10px;
        .p1 {
            color: rgb(78, 78, 78);
        }
        .p2 {
            color: red;
        }
    }
    /deep/.goods_desc {
        img {
            max-width: 100%;
            display: block;
        }
    }
    .attribute {
        font-size: 13px;
        margin-top: 0.2rem;
        background: #fff;
        padding: 0.2rem 4%;
        h4 {
            font-weight: normal;
            font-size: 16px;
            margin-bottom: 20px;
        }
        ul {
            li {
                background: #efefef;
                line-height: 0.4rem;
                height: 0.4rem;
                margin-bottom: 0.1rem;
                border-radius: 4px;
                display: flex;
                justify-content: space-between;
                span {
                    width: 30%;
                    text-align: right;
                }
                section {
                    flex: 1;
                    padding-left: 0.2rem;
                }
            }
        }
    }
    // 常见问题
    .issue {
        padding: 0 4%;
        background: #fff;
        li {
            h3 {
                height: 50px;
                line-height: 50px;
                padding-left: 20px;
                font-size: 16px;
                position: relative;
                font-weight: normal;
                &::before {
                    content: '';
                    width: 4px;
                    height: 4px;
                    background: darkred;
                    display: inline-block;
                    position: absolute;
                    left: 0;
                    top: 50%;
                    margin-top: -2px;
                }
            }
            p {
                font-size: 12px;
                line-height: 20px;
                padding-left: 20px;
            }
        }
    }
    .title {
        width: 100%;
        background: #fff;
        height: 50px;
        position: relative;
        &::before {
            content: '';
            width: 50%;
            height: 2px;
            background: #ccc;
            position: absolute;
            top: 50%;
            margin-top: -1px;
            left: 50%;
            margin-left: -25%;
        }
        span {
            width: 30%;
            position: relative;
            background: #fff;
            display: block;
            text-align: center;
            margin: 0 auto;
            height: 50px;
            line-height: 50px;
        }
    }
    // 相关产品
    .newList {
        ul {
            font-size: 16px;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            li {
                width: 49%;
                position: relative;
                margin-bottom: 5px;
                span {
                    display: block;
                    margin: 0;
                    text-align: center;
                }
                p {
                    display: block;
                    margin: 0;
                    text-align: center;
                    color: red;
                }
                img {
                    width: 100%;
                }
            }
        }
    }
}
</style>
